<!--
 * @Author: your name
 * @Date: 2022-03-28 20:17:00
 * @LastEditTime: 2022-09-04 09:02:06
 * @LastEditors: xiaobaobeibai 604070135@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /admin-web/src/views/user_management/components/modal.vue
-->
<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="580px" :before-close="handleClose">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" class="demo-ruleForm">
      <el-form-item label="时间" prop="year">
        <el-date-picker
          v-model="ruleForm.year"
          type="year"
          style="width:100%"
          value-format="yyyy"
          format="yyyy"
          placeholder="选择时间"
        />
      </el-form-item>
      <el-form-item label="公司简介" prop="sort">
        <Editor ref="editor" :clear-edit="clearEdit" :content="ruleForm.content" />
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resetForm('ruleForm')">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { REQUEST_LIST } from '@/api/requestList'
import Editor from '@/components/wangeditor/wangeditor_bendi.vue'
export default {
  components: { Editor },
  data() {
    return {
      dialogVisible: false,
      title: '',
      clearEdit: true,
      page: '',
      ruleForm: {
        content: '',
        year: ''
      },
      rules: {
        year: [{ required: false, message: '请选择年份', trigger: 'blur' }]
      }
    }
  },
  methods: {
    open(page, data) {
      this.dialogVisible = true
      this.page = page
      if (page === 'edit') {
        this.clearEdit = true
        this.ruleForm = data
      } else {
        this.clearEdit = false
      }
      this.title = '提示'
    },
    handleClose() {
      this.dialogVisible = false
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {}
      // this.clearEdit = true
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.img = res.data.fileName
      this.ruleForm.imgUrl = res.data.previewUrl + '/' + res.data.fileName
    },

    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.page === 'add') {
            this.ruleForm.content = this.$refs.editor.editorContent
            REQUEST_LIST.REQUST_FCN('COMPANYFAZHANADD', this.ruleForm, true).then(
              (res) => {
                if (res.status === 200) {
                  this.$emit('ok')
                  this.$message.success('添加成功！')
                  this.handleClose()
                }
              }
            )
          } else {
            const data = {
              content: this.$refs.editor.editorContent,
              id: this.ruleForm.id,
              year: this.ruleForm.year
            }
            REQUEST_LIST.REQUST_FCN('COMPANYFAZHANEDIT', data, true).then(
              (res) => {
                if (res.status === 200) {
                  this.$emit('ok')
                  this.$message.success('编辑成功！')
                  this.handleClose()
                }
              }
            )
          }
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.handleClose()
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
